//
//  HLMacro.h
//  s.
//
//  Created by fanshijian on 2022/8/23.
//  Copyright © 2022 J. All rights reserved.
//

#ifndef HLMacro_h
#define HLMacro_h


#define main_queue   dispatch_get_main_queue()
#define global_queue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define kAppDelegate ((AppDelegate*)[[UIApplication sharedApplication] delegate])

#define kError(cod, desc) \
[NSError errorWithDomain:([NSString stringWithFormat:@"%@.HLErrorDomain", [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]]) code:cod userInfo:@{NSLocalizedDescriptionKey : desc}]

//MARK: - 通知
#define kLogoutNotification     @"kLogoutNotification"
#define kLoginNotification      @"kLoginNotification"


//MARK: - 常量
#define kPlaceHolderColor   ([KLColor(@"#000000") colorWithAlphaComponent:0.2]) // 右侧文本占位符颜色
#define kDescColor          (KLColor(@"#000000")) // 右侧文本有值 & 可编辑
#define kDescBanColor       ([KLColor(@"#000000") colorWithAlphaComponent:0.4]) // 右侧文本有值 & 不可编辑



#define kPrivacyUrl @"https://lightthingscom.mm/privacy-statement.html"
#define kAgreementUrl @"https://lightthingscom.mm/registration-agreement.html"


#pragma mark - Directory

#define _HYBIRD_PATH_LIBRARY    [NSHomeDirectory() stringByAppendingPathComponent:@"Library"]

#define _HYBIRD_PATH_DOCUMENT    [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]

#define _HYBIRD_PATH_CACHE      [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"Cache"]

#define _HYBIRD_PATH_DATA [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"DATA"]

#define _HYBIRD_PATH_VERSONPDIR [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"DATA/VERSON"]

#define _HYBIRD_PATH_CURRENTVERSON [_HYBIRD_PATH_VERSONPDIR stringByAppendingFormat:@"/%@",app_version]

#define _HYBIRD_PATH_IMAGE [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"IMAGES"]

#define AUDIO_PATH      [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"Audio"]

#define VIDEO_PATH      [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"Video"]

#define RECORDER_PATH   [_HYBIRD_PATH_DOCUMENT stringByAppendingPathComponent:@"Recorder"]


#pragma --mark 消息处理 ----
//全局消息处理, 带参，不带参
#define kRunCMD(cmd)          @{@"app_action":(cmd)}
#define kRunCMDINFO(cmd, ...) @{@"app_action":(cmd),@"info":__VA_ARGS__}
//自定义消息处理, 带参，不带参
#define kMsg(cmd)                           @{@"msg":(cmd)}
#define kMsgPack(cmd, ...)                  @{@"msg":(cmd),@"info":__VA_ARGS__}
#define kMsgForward(cmd, target)            @{@"msg":(cmd),@"target":(target)}
#define kMsgPackForward(cmd,target, ...)    @{@"msg":(cmd),@"target":(target),@"info":__VA_ARGS__}

#define ZHRunEngine(info, handle)\
Class cls = (id)objc_getClass("JYRunEngine"); \
NSAssert(cls != nil, @"无法找到类HLRunEngine"); \
((void(*)(id,SEL,id,id))objc_msgSend)(cls,NSSelectorFromString(@"handleCommand:handle:"),(info),(handle));

#define ZHRun(_sel_, ...) [self executeSelectorWithArgs:(NSSelectorFromString(@#_sel_)), ##__VA_ARGS__]
#define ZHObjRun(_obj_, _sel_, ...) [(_obj_) executeSelectorWithArgs:(NSSelectorFromString(@#_sel_)), ##__VA_ARGS__]

#define notice(_name, _obj, info) [[NSNotificationCenter defaultCenter] postNotificationName:(_name) object:(_obj) userInfo:(info)]
#define notice_add(self,sel,_name,_obj) [[NSNotificationCenter defaultCenter] addObserver:(self) selector:(sel) name:(_name) object:(_obj)]
#define notice_remove_all(self) [[NSNotificationCenter defaultCenter] removeObserver:(self)];
#define notice_remove(self,_name,_obj) [[NSNotificationCenter defaultCenter] removeObserver:(self) name:(_name) object:(_obj)]

#pragma --mark Bundle image ----
#define KLMainBundleImagePath(img, _type) [NSBundle.mainBundle pathForScaledResource:(img) ofType:(_type) ?: @"png"]
#define KLIMAGES_BUNDLE [NSBundle bundleWithPath:[[NSBundle mainBundle] pathForResource:@"Images" ofType:@"bundle"]]
#define KLImageFromImagesBundle(img, type) [KLIMAGES_BUNDLE pathForScaledResource:(img) ofType:(type)]


#pragma --mark App keys ----
#if DEBUG
#define KL_APP_KEYS   jy_app_Info()[@"KEYID_Debug"]
#else
#define KL_APP_KEYS   jy_app_Info()[@"KEYID_Release"]
#endif

#pragma --mark Debug About ----
#ifdef DEBUG
#define ISDEBUG (1)
#else
#define ISDEBUG (0)
#endif

#ifdef DEBUG
//#define HLSLog(fmt, ...) NSLog((@"[Service] " fmt), ##__VA_ARGS__)
#define HLSLog(fmt, ...) HLFileLog((@"[Service] " fmt), ##__VA_ARGS__)
#else
#define HLSLog(...)
#endif

#ifdef DEBUG
#define HLIMLog(fmt, ...) NSLog((@"[IM] " fmt), ##__VA_ARGS__)
#else
#define HLIMLog(...)
#endif

#ifdef DEBUG
#define HLInfoLog(fmt, ...) NSLog((@"[INFO] " fmt), ##__VA_ARGS__)
#else
#define HLInfoLog(...)
#endif

#if DEBUG
#define JYLog(fmt, ...) NSLog((@"%s[L:%d]: " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define JYLog(...)
#endif

#ifdef DEBUG
#define HLTimeLog(fmt, ...) NSLog((@"[TIME] " fmt), ##__VA_ARGS__)
#else
#define HLTimeLog(...)
#endif

//app版本
#define app_version jy_app_Info()[@"CFBundleShortVersionString"]
#define app_system @"ios"

#define kStringFormat(...) [NSString stringWithFormat:__VA_ARGS__]
#define klSafeParameter(parameter) ((!parameter || parameter == (id)kCFNull) ? @"" : (parameter))
#define klSafeParameterSpace(parameter) ((!parameter || parameter == (id)kCFNull) ? @" " : (parameter))

/// Null数据校验
#define kNullCheck(obj)\
({\
    BOOL isOk;\
    if (obj == nil)\
        isOk = NO;\
    else if ([obj isKindOfClass:[NSNull class]]) \
        isOk = NO;\
    else\
        isOk = YES;\
    (isOk);\
})\


#define HLFileLog(FORMAT, ...)\
({\
if(ISDEBUG){\
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];\
    fmt.timeZone = [NSTimeZone localTimeZone];\
    fmt.locale = [[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];\
    [fmt setDateFormat:@"yyyy-MM-dd HH:mm:ss"];\
    const char *date = [[fmt stringFromDate:[NSDate date]] UTF8String];\
    const char *file = [[[NSString stringWithUTF8String: __FILE__] lastPathComponent] UTF8String];\
    int line = __LINE__;\
    fprintf(stderr, "[%s,%s,%d] %s\n", date,file,line,[[NSString stringWithFormat: FORMAT, ## __VA_ARGS__] UTF8String]);}\
else{\
}\
})\


#endif /* HLMacro_h */


